package com.gateaway.com.config;

import com.alibaba.cloud.seata.feign.SeataFeignClientAutoConfiguration;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import io.seata.spring.boot.autoconfigure.SeataAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @author
 * @version 1.0
 * @description: TODO
 * @date 2022/10/24 16:35
 */
//不需要主动装配数据源和seata
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
        SeataAutoConfiguration.class, DruidDataSourceAutoConfigure.class,SeataFeignClientAutoConfiguration.class})
@Slf4j
@EnableFeignClients
@EnableDiscoveryClient
public class GateWayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class,args);
    }

    @Bean("ipKeyResolver")
    public KeyResolver userKeyResolver(){
        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                //获取用户ip
                String hostString = Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostString();
                log.info("登录gateway用户ip为:{}",hostString);
                return Mono.just(hostString);
            }
        };
    }



}
